package com.serookie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serookie.config.security.JwtTokenUtils;
import com.serookie.entity.Result;
import com.serookie.entity.SysUser;
import com.serookie.entity.WxAuthProperties;
import com.serookie.entity.WxUser;
import com.serookie.mapper.SysUserMapper;
import com.serookie.mapper.WxUserMapper;
import com.serookie.service.SysUserService;
import com.serookie.service.WxUserService;
import com.serookie.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author kevintam
 * @version 1.0
 * @date 2021/5/31 20:28
 */
@Service
public class WxUserServiceImpl extends ServiceImpl<WxUserMapper,WxUser> implements WxUserService{

    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private WxUserMapper wxUserMapper;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Result WxLogin(String code,SysUser user,HttpServletRequest request) {
        if(code==null){
            return Result.error().message("code值不能为空");
        }
        String path = "/sns/jscode2session";
        String method = "POST";
        String grant_type = "authorization_code";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        //根据API的要求，定义相对应的Content-Type
        headers.put("content-type", "application/json");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("appid", WxAuthProperties.mini_appID);
        bodys.put("secret", WxAuthProperties.mini_secret);
        bodys.put("js_code", code);
        bodys.put("grant_type", grant_type);
        try {
            HttpResponse response = HttpUtils.doPost(WxAuthProperties.sessionHost, path, method, headers, querys, bodys);
            String toString = EntityUtils.toString(response.getEntity());
            JSONObject responseBody = new JSONObject(toString);
            String openid = responseBody.getString("openid");
            QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
            wrapper.eq("openid",openid);
            SysUser sysUser = userMapper.selectOne(wrapper);
            System.out.println(sysUser);
            if(sysUser!=null){
                Result token = sysUserService.login(openid, openid, request);
                System.out.println(openid);
                return token;
            }else {
                user.setOpenid(openid);
                user.setUsername(openid);
                user.setPassword(passwordEncoder.encode(openid));
                int insert = userMapper.insert(user);
                Result token = sysUserService.login(openid, openid, request);
                return token;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<WxUser> all() {
        List<WxUser> wxUsers = wxUserMapper.selectList(null);
        return wxUsers;
    }

//    /**
//     * 查询所有的用户
//     * @return
//     */
//    @Override
//    public List<WxUser> all() {
//        return userMapper.selectList(null);
//    }
//
//    /**
//     * 添加微信用户
//     * @param user
//     * @return
//     */
//    @Override
//    public Boolean addWxUser(WxUser user) {
//        return userMapper.insert(user)>0;
//    }
//
//    @Override
//    public WxUser getOpenid(String openid) {
//        return userMapper.selectById(openid);
//    }

}
